using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using NTTHAO.Common.BL;
using NTTHAO.CommonWeb;

public partial class admin_webparts_Language_LanguageEdit : System.Web.UI.UserControl
{
    public string _ErrorMessage = String.Empty;

    protected void Page_Load(object sender, EventArgs e)
    {
        btnSave.Text = LanguageUtility.GetLanguageText("SAVE", Session);
        btnSave2.Text = LanguageUtility.GetLanguageText("SAVE", Session);

        if (!IsPostBack)
        {
            if ("edit".Equals(Request.QueryString["action"], StringComparison.OrdinalIgnoreCase))
            {
                txtName.Enabled = false;
                try
                {
                    string idStr = Request.QueryString["id"].Trim();
                    LanguageText lg = new LanguageText(Int32.Parse(idStr));
                    txtName.Text = lg.Name;
                    txtValue.Text = lg.Text;
                    txtType.Text = lg.Type.ToString();
                }
                catch (Exception)
                {
                    Response.Redirect("?type=lang&action=list");
                }
            }
            else if ("add".Equals(Request.QueryString["action"], StringComparison.OrdinalIgnoreCase))
            {
                txtName.Enabled = true;
            }
            else
            {
                Response.Redirect("?type=lang&action=list");
            }
        }
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        try
        {
            LanguageText lg = null;

            if ("edit".Equals(Request.QueryString["action"], StringComparison.OrdinalIgnoreCase))
            {
                string idStr = Request.QueryString["id"].Trim();
                lg = new LanguageText(Int32.Parse(idStr));
            }
            else
            {
                lg = new LanguageText();
                lg.Name = txtName.Text;
                lg.LanguageID = LanguageUtility.GetLanguageID(Request, Session);
            }

            lg.Text = txtValue.Text;
            lg.Type = Int32.Parse(txtType.Text.Trim());

            if ("edit".Equals(Request.QueryString["action"], StringComparison.OrdinalIgnoreCase))
            {
                lg.Update();
            }
            else
            {
                lg.Insert();
            }

            // reload session language
            NTTHAO.CommonWeb.LanguageUtility.ReloadLanguage(Request, Response, Session);
            // end reload

            Response.Redirect("?type=lang&action=list");
        }
        catch (Exception ex)
        {
            this._ErrorMessage = ex.Message;
        }
    }
}
